package statemachine;

import org.squirrelframework.foundation.fsm.StateMachineBuilderFactory;
import org.squirrelframework.foundation.fsm.StateMachineConfiguration;
import org.squirrelframework.foundation.fsm.UntypedStateMachine;
import org.squirrelframework.foundation.fsm.UntypedStateMachineBuilder;
import org.squirrelframework.foundation.fsm.annotation.StateMachineParameters;
import org.squirrelframework.foundation.fsm.impl.AbstractUntypedStateMachine;

/**
 * @author dushougudu
 */
public class SquirrelStateMachine<T extends UntypedStateMachine> {

    @StateMachineParameters(stateType = String.class, eventType = String.class, contextType = Integer.class)
    static class StateMachineSample extends AbstractUntypedStateMachine {
        protected void fromAToB(String from, String to, String event, Integer context) {
            System.out.println("Transition from '" + from + "' to '" + to + "' on event '" + event +
                    "' with context '" + context + "'.");
        }

        protected void ontoB(String from, String to, String event, Integer context) {
            System.out.println("Entry State \'" + to + "\'.");
        }
    }

    public static void main(String[] args) {
        UntypedStateMachineBuilder builder = StateMachineBuilderFactory.create(StateMachineSample.class);
        builder.setStateMachineConfiguration(StateMachineConfiguration.create().enableDebugMode(false));
        builder.externalTransition().from("A").to("B").on("toB").callMethod("fromAToB");
        builder.onEntry("B").callMethod("ontoB");

        //Use State Machine
        UntypedStateMachine fsm = builder.newStateMachine("B");
        fsm.fire("toB", 10);

        System.out.println("Current state is " + fsm.getCurrentState());
    }
}
